/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.common.component.log;

import com.stevejrong.waiter.common.component.BaseConstants;
import com.stevejrong.waiter.common.component.util.JsonUtil;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 工具类 - 日志构建器
 *
 * @author Steve Jrong
 * @since 1.0
 */
@NoArgsConstructor
public final class LogBuilder {
	private static final String VERTICAL_LINE_SPLITERATOR = BaseConstants.SymbolConstants.SYMBOL_SPACE.desc()
																+ BaseConstants.SymbolConstants.SYMBOL_VERTICAL_LINE.desc()
																+ BaseConstants.SymbolConstants.SYMBOL_SPACE.desc();
	
    /**
     * 自定义键
     */
    private String key;

    /**
     * 自定义值
     */
    private Object value;

    /**
     * 自定义值集合
     */
    private Collection<?> collection;
    private final StringBuffer logStringAppender = new StringBuffer();

    public LogBuilder(String key, Object value) {
        this.key = key;
        this.value = value;
        this.append(key, value);
    }

    public LogBuilder(String key, Collection<?> collection) {
        this.key = key;
        this.collection = collection;
        this.append(key, collection);
    }

    public LogBuilder(Map<String, ?> map) {
        this.append(map);
    }

    public LogBuilder append(String key, Object value) {
        this.logStringAppender.append(BaseConstants.SymbolConstants.SYMBOL_LEFT_SQUARE_BRACKETS.desc())
                .append(key)
                .append(BaseConstants.SymbolConstants.SYMBOL_COLON.desc())
                .append(JsonUtil.objectToJsonString(value))
                .append(BaseConstants.SymbolConstants.SYMBOL_RIGHT_SQUARE_BRACKETS.desc())
                .append(VERTICAL_LINE_SPLITERATOR);
        return this;
    }

    public LogBuilder append(String key, Collection<?> collection) {
        this.logStringAppender.append(BaseConstants.SymbolConstants.SYMBOL_LEFT_SQUARE_BRACKETS.desc())
                .append(key)
                .append(BaseConstants.SymbolConstants.SYMBOL_COLON.desc())
                .append(JsonUtil.objectToJsonString(collection))
                .append(BaseConstants.SymbolConstants.SYMBOL_RIGHT_SQUARE_BRACKETS.desc())
                .append(VERTICAL_LINE_SPLITERATOR);
        return this;
    }

    public LogBuilder append(Map<String, ?> map) {
        for (Map.Entry<String, ?> item : map.entrySet()) {
            this.append(item.getKey(), item.getValue());
        }
        return this;
    }

    public String toString() {
        String logString = this.logStringAppender.toString();
        return logString.contains(VERTICAL_LINE_SPLITERATOR)
                ? StringUtils.substringBeforeLast(logString, VERTICAL_LINE_SPLITERATOR) : logString;
    }
}